/**
 *  GODPAPER Confidential,Copyright 2012. All rights reserved.
 *
 *  Permission is hereby granted, free of charge, to any person obtaining
 *  a copy of this software and associated documentation files (the "Software"),
 *  to deal in the Software without restriction, including without limitation
 *  the rights to use, copy, modify, merge, publish, distribute, sub-license,
 *  and/or sell copies of the Software, and to permit persons to whom the
 *  Software is furnished to do so, subject to the following conditions:
 *
 *  The above copyright notice and this permission notice shall be included
 *  in all copies or substantial portions of the Software.
 *
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 *  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 *  IN THE SOFTWARE.
 */
package
{
	import com.godpaper.mqtt.as3.core.Log;
	import com.godpaper.mqtt.as3.core.MQTTEvent;
	import com.godpaper.mqtt.as3.impl.MQTTSocket;
	import com.godpaper.mqtt.as3.utils.Base64;
	import com.godpaper.mqtt.as3.utils.UIDUtil;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.system.Security;
	import flash.utils.ByteArray;
	import flash.utils.setTimeout;
	
	/**
	 * Pure Action Script 3 that implements the MQTT (Message Queue Telemetry Transport) protocol, a lightweight protocol for publish/subscribe messaging. </br>
	 * AS3 socket is a mechanism used to send data over a network (e.g. the Internet), it is the combination of an IP address and a port. </br>
	 * @see https://github.com/yangboz/as3MQTT
	 * @see https://github.com/yangboz/as3MQTT/wiki
	 * @see https://github.com/yangboz/as3Logger
	 * 
	 * @author yangboz
	 * @langVersion 3.0
	 * @playerVersion 11.2+
	 * @airVersion 3.2+
	 * Created Nov 20, 2012 10:19:53 AM
	 */
	public class MQTT_DEBUG extends Sprite
	{
		protected const baseStr:String="0123456789";
		protected var tempStr:String;

		private var mqttSocket:MQTTSocket;

		//Notice: You need to define a cross domain policy file at your remote server root document, or have a policy file server on the target. 
		private static const MY_HOST:String="post-cn-mp90pr6cx0q.mqtt.aliyuncs.com"; //You'd better change it to your private ip address! //test.mosquitto.org//16.157.65.23(Ubuntu)//15.185.106.72(hp cs instance)
		private static const MY_PORT:Number=1883; //Socket port.
		
//		private static const MY_HOST:String="139.129.44.147";
//		private static const MY_PORT:Number=1883; //Socket port.

		public function MQTT_DEBUG()
		{
			//			Creating a Socket
			this.mqttSocket = new MQTTSocket();
			this.mqttSocket.clientid = "GID_t01@@@29";
			Log.setDebug(true);
//			this.mqttSocket.username = "LTAIV7k2ORoUSufc";
//			this.mqttSocket.password = "73QX/TqQenh+XlPDj+st1lwot+w=";
			
			//Notice: You need to define a cross domain policy file at your remote server root document, or have a policy file server on the target. 
//			Security.allowDomain("*");
			//			Security.loadPolicyFile("http://www.lookbackon.com/crossdomain.xml");  
			//event listeners
			mqttSocket.addEventListener(MQTTEvent.CONNECT, onConnect); //dispatched when the connection is established
			mqttSocket.addEventListener(MQTTEvent.CLOSE, onClose); //dispatched when the connection is closed
			mqttSocket.addEventListener(MQTTEvent.ERROR, onError); //dispatched when an error occurs
			mqttSocket.addEventListener(MQTTEvent.MESSGE, onMessage); //dispatched when socket can be read

			mqttSocket.connect(MY_HOST, MY_PORT);
//			this.addEventListener("PUB",pub);
		}
		
		private function pub_s():void
		{
			this.tempStr += this.baseStr;
			this.mqttSocket.publish("eslive_tst/41/normal_recv",this.tempStr);
		}
		
		private function pub(e:Event):void
		{
			pub_s();
		}

		private function onConnect(event:MQTTEvent):void
		{
			var data:Object = {
				token : "LzMT+XLFl5uQ6bDU0o4vUqOS0TJurC+xAJFqO9IwvppYfYNDKkKbsA/lDnhfVjgSTLyohRU9uIz3Tdh9/DenfwLCMhKhYKVdRrBibcttXpnJp70ygHwN9q8K1cKI6a9iEGorBlDMXM5AoY2q1cbdtFh5PedX8EQGoBvPr9ET6PKvzS6eC74DxytYizH8jrvZifBRkIOi1gEQaisGUMxczt6dtZeC2xik1oC64FBcBlVaWuTZozm/m0P5IyQnJHVketHW0v9gwhZ2q/Jps4XXvRLbBtk5tYcQ2TC+SmCHYpeINj6FMvT3fAhaxD3pCvEOYc6BKP1yKBxr8P5KCuQg2J8C/1Hx7pbf",
				type : "RW"
			};
			mqttSocket.publish("$SYS/uploadToken",JSON.stringify(data));
			mqttSocket.subscribe(["eslive_tst/61/vip_recv"], [0], 1);//必须是这个，不能动啊，ok
			
		}
		
		private function onClose(event:MQTTEvent):void
		{
			Log.log("MQTT close: {0}",event.message);
		}
		
		private function onError(event:MQTTEvent):void
		{
			Log.log("MQTT Error: {0}",event.message);
		}
		
		private function onMessage(event:MQTTEvent):void
		{			;
			Log.info(Base64.encode(event.message));
//			this.dispatchEvent(new Event("PUB"));

		}
		
	}
	
}
